using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using SimpleToDoList.Models;

namespace SimpleToDoList.Services;

public static class ToDoListFileService {
    private static string _jsonFileName =
        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Avalonia.SimpleToDoList",
            "MyToDoList.json");

    public static async Task SaveToFileAsync(IEnumerable<ToDoItem> itemsToSave) {
        Directory.CreateDirectory(Path.GetDirectoryName(_jsonFileName)!);
        using (var fs = File.Create(_jsonFileName)) {
            await JsonSerializer.SerializeAsync(fs, itemsToSave);
        }
    }
}